/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services.implementation.paypal;

import com.paypal.sdk.profiles.APIProfile;
import com.paypal.sdk.profiles.ProfileFactory;
import com.paypal.sdk.services.CallerServices;
import com.paypal.soap.api.BasicAmountType;
import com.paypal.soap.api.CurrencyCodeType;
import com.paypal.soap.api.DoExpressCheckoutPaymentRequestDetailsType;
import com.paypal.soap.api.DoExpressCheckoutPaymentRequestType;
import com.paypal.soap.api.DoExpressCheckoutPaymentResponseType;
import com.paypal.soap.api.PaymentActionCodeType;
import com.paypal.soap.api.PaymentDetailsType;

/**
 *
 * @author smile
 */
public class ECDoExpressCheckout 
{	
    public String ECDoExpressCheckoutCode(String token, String payerID, String paymentAmount, 
                                                    PaymentActionCodeType paymentAction, CurrencyCodeType currencyCodeType)
    {
        String responseValue = null;
        CallerServices caller = new CallerServices();

        try
        {
                APIProfile profile = ProfileFactory.createSignatureAPIProfile();
                /*
                 WARNING: Do not embed plaintext credentials in your application code.
                 Doing so is insecure and against best practices.
                 Your API credentials must be handled securely. Please consider
                 encrypting them for use in any production environment, and ensure
                 that only authorized individuals may view or modify them.
                 */

        // Set up your API credentials, PayPal end point, and API version.
                profile.setAPIUsername("Sel_01_1309787097_biz_api1.gmail.com");
                profile.setAPIPassword("1309787136");
                profile.setSignature("A3vsavgqWrm5eIuZDJ5rocMWlCRTAEXhkMekgy5oFv-JSx.a665Qj0s.");
                profile.setEnvironment("sandbox");
                caller.setAPIProfile(profile);

                // Create the request object.
                DoExpressCheckoutPaymentRequestType pprequest = new DoExpressCheckoutPaymentRequestType();
                pprequest.setVersion("51.0");

        // Add request-specific fields to the request.
                DoExpressCheckoutPaymentResponseType ppresponse=new DoExpressCheckoutPaymentResponseType();

                // Create the request details object.
                DoExpressCheckoutPaymentRequestDetailsType paymentDetailsRequestType = new DoExpressCheckoutPaymentRequestDetailsType();
                //pass the token value by actual value returned in the SetExpressCheckout
                paymentDetailsRequestType.setToken(token);
                paymentDetailsRequestType.setPayerID(payerID);
                paymentDetailsRequestType.setPaymentAction(paymentAction);



                BasicAmountType orderTotal = new BasicAmountType();
                orderTotal.set_value(paymentAmount);
                orderTotal.setCurrencyID(currencyCodeType);


                PaymentDetailsType[] details = new PaymentDetailsType[1];
                PaymentDetailsType paymentDetails = new PaymentDetailsType();



                paymentDetails.setOrderTotal(orderTotal);
                details[0] = paymentDetails;

                paymentDetailsRequestType.setPaymentDetails(details);// setPaymentDetails(paymentDetails);
                pprequest.setDoExpressCheckoutPaymentRequestDetails(paymentDetailsRequestType);

            // Execute the API operation and obtain the response.
                ppresponse= (DoExpressCheckoutPaymentResponseType) caller.call("DoExpressCheckoutPayment", pprequest);
                responseValue = ppresponse.getAck().toString();

                System.out.println(ppresponse.getCorrelationID().toString() + " -> getCorrelationID");
                System.out.println(ppresponse.getAck().toString() + " -> getAck");

        }catch(Exception ex)
        {
                ex.printStackTrace();
        }

        return responseValue;
    }
}
